import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
            showTabBar: true
        },
        children: [{
            path: "/home",
            redirect: "/home/index"
        }, {
            path: "index",
            name: "Index",
            component: () =>
                import ('../views/Home/children/Index.vue'),
            meta: {
                showTabBar: true
            },
        }, {
            path: "other",
            name: "Other",
            component: () =>
                import ('../views/Home/children/Other.vue'),
            meta: {
                showTabBar: true
            },
        }]
    },
    {
        path: '/category',
        name: 'Category',
        component: () =>
            import ('../views/Category/Category.vue'),
        meta: {
            showTabBar: true
        },
    },
    {
        path: '/find',
        name: 'Find',
        component: () =>
            import ('../views/Find/Find.vue'),
        meta: {
            showTabBar: true
        }
    },
    {
        path: '/cart',
        name: 'Cart',
        component: () =>
            import ('../views/Cart/Cart.vue'),
        meta: {
            showTabBar: true
        }
    },
    {
        path: '/mine',
        name: 'Mine',
        component: () =>
            import ('../views/Mine/Mine.vue'),
        meta: {
            showTabBar: true
        }
    },
    {
        path: '/userlogin',
        name: 'UserLogin',
        component: () =>
            import ('../views/Login/UserLogin.vue')
    },
    {
        path: '/phonelogin',
        name: 'PhoneLogin',
        component: () =>
            import ('../views/Login/PhoneLogin.vue') //按需引入
    }, {
        path: '/adduser',
        name: 'Adduser',
        component: () =>
            import ('../views/Login/Adduser.vue') //按需引入
    }, {
        path: '/categorylist/:cat_id',
        name: 'CategoryList',
        component: () =>
            import ('../views/Category/components/CategoryList.vue')
    }, {
        path: '/detail/:goods_id',
        name: 'Detail',
        component: () =>
            import ('../views/Detail/Detail.vue')
    }, {
        path: "/",
        redirect: "/userlogin"
    }
]
const router = new VueRouter({
    routes
})


//路由守卫
router.beforeEach((to, form, next) => {
    //to:即将进入的目标
    //from:即将离开的路由
    // next：放行
    var token = localStorage.getItem("token");
    if (to.path == "/userlogin" || to.path == "/phonelogin") {
        next();
        return;
    }
    //如果token不存在，跳转到登录页面
    if (!token) return next("/userlogin");
    next();
})
export default router